﻿Public Class ucNFPesquisa


#Region "Eventos públicos acessados pelo formulário de cadastro"
    Public Event Incluir()
    Public Event Sair()
    Public Event Visualizar(ByVal nf As Model.NfPesquisa)
#End Region

#Region "Metodos privados do UC"
    Private Sub Pesquisa()
        Dim ctrl As New Control.Nf

        ' Fazer if para pesquias por fabricante, barra e nome
        ' ListaByNome
        ' ListaByFabricante
        ' ListaByBarra

        bsNFPesquisa.DataSource = ctrl.ListaTudoPesquisa()
        dgvNFPesquisa.DataSource = bsNFPesquisa
    End Sub

    Private Sub Exclui()
        If CType(bsNFPesquisa.Current, Model.NfPesquisa) IsNot Nothing Then
            Dim ctrlNF As New Control.Nf
            If Dialog.QuestionYesNo("Deseja realmente remover este registro?") = Windows.Forms.DialogResult.Yes And Not bsNFPesquisa.Current Is Nothing Then
                If Not ctrlNF.ExcluirPesquisa(CType(bsNFPesquisa.Current, Model.NfPesquisa)) Then
                    MessageBox.Show("Não foi possivel remover pois há relacionamentos!")
                End If
            End If

            ctrlNF = Nothing
            Pesquisa()
        End If
    End Sub

    Private Sub Visualiza()
        If CType(bsNFPesquisa.Current, Model.NfPesquisa) IsNot Nothing Then
            RaiseEvent Visualizar(CType(bsNFPesquisa.Current, Model.NfPesquisa))
        End If
    End Sub

    ''' <summary>
    ''' Envia NFes selecionadas
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub NFeEnvia()
        ' Cria camada de controle
        Dim controlNFe As New Control.NFe
        Dim nfs As New List(Of Model.NfPesquisa)

        ' NFes selecionadas
        Dim nfsPesquisa As IList(Of Model.NfPesquisa)
        nfsPesquisa = CType(bsNFPesquisa.DataSource, IList(Of Model.NfPesquisa))
        For Each nf In nfsPesquisa
            If nf.nf_selecao Then
                nfs.Add(nf)
            End If
        Next

        ' Envia a camada de controle
        controlNFe.NFeEnvia(nfs)
    End Sub

    Private Sub Seleciona()
        If CType(bsNFPesquisa.Current, Model.NfPesquisa) IsNot Nothing Then
            Dim value As Boolean
            value = CType(bsNFPesquisa.Current, Model.NfPesquisa).nf_selecao
            CType(bsNFPesquisa.Current, Model.NfPesquisa).nf_selecao = Not value
            dgvNFPesquisa.Refresh()
        End If
    End Sub

    Private Sub NFeStatusServico()
        Dim ct As New Control.NFe
        ct.NFeGeraPedidoStatusServico()
    End Sub

#End Region


    Private Sub btnPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPesquisar.Click
        Pesquisa()
    End Sub

    Private Sub btnSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSair.Click
        RaiseEvent Sair()
    End Sub

    Private Sub btnIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIncluir.Click
        RaiseEvent Incluir()
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizar.Click
        Visualiza()
    End Sub

    Private Sub btnRemover_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemover.Click
        Exclui()
    End Sub

    Private Sub dgvNFPesquisa_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvNFPesquisa.DoubleClick
        Visualiza()
    End Sub

    Private Sub dgvNFPesquisa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvNFPesquisa.Click
        Seleciona()
    End Sub

    Private Sub NFeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NFeToolStripMenuItem.Click
        NFeEnvia()
    End Sub

    Private Sub StatusServiçoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusServiçoToolStripMenuItem.Click
        NFeStatusServico()
    End Sub
End Class
